﻿using System.Windows.Controls.Primitives;

namespace Profit.Controls
{
    using System.Windows.Controls;
    using System.Windows;
    using System.Windows.Input;

    /// <summary>
    /// Interaction logic for DropDownButtonControl.xaml
    /// </summary>
    public partial class DropDownButtonControl : UserControl
    {
        public static DependencyProperty ButtonContentProperty = DependencyProperty.Register("ButtonContent", typeof(object), typeof(DropDownButtonControl), new PropertyMetadata((object)null));
        public static DependencyProperty ButtonCommandProperty = DependencyProperty.Register("ButtonCommand", typeof(ICommand), typeof(DropDownButtonControl), new PropertyMetadata((ICommand)null));
        public static DependencyProperty ButtonCommandParameterProperty = DependencyProperty.Register("ButtonCommandParameter", typeof(object), typeof(DropDownButtonControl), new PropertyMetadata((object)null));
        public static DependencyProperty ShowPopupProperty = DependencyProperty.Register("ShowPopup", typeof(bool), typeof(DropDownButtonControl), new PropertyMetadata((bool)false, OnShowPopupChanged));
        public static DependencyProperty ShowDropDownIconProperty = DependencyProperty.Register("ShowDropDownIcon", typeof(bool), typeof(DropDownButtonControl), new PropertyMetadata((bool)true));

        public DropDownButtonControl()
        {
            InitializeComponent();
        }


        public bool ShowDropDownIcon
        {
            get { return (bool) GetValue(ShowDropDownIconProperty); }
            set { SetValue(ShowDropDownIconProperty,value); }
        }

        public bool ShowPopup 
        {
            get { return (bool)GetValue(ShowPopupProperty); }
            set { SetValue(ShowPopupProperty, value); }
        
        }

        public object ButtonContent
        {
            get { return GetValue(ButtonContentProperty); }
            set { SetValue(ButtonContentProperty,value); }
        }

        public ICommand ButtonCommand
        {
            get { return GetValue(ButtonCommandProperty) as ICommand; }
            set { SetValue(ButtonCommandProperty, value); }
        }

        public object ButtonCommandParameter
        {
            get { return GetValue(ButtonCommandParameterProperty); }
            set { SetValue(ButtonCommandParameterProperty, value); }
        }

        private static void OnShowPopupChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
        {
            var ddbtn = o as DropDownButtonControl;
            if(ddbtn !=null && ddbtn.ContextMenu !=null)
            {
                ddbtn.ContextMenu.PlacementTarget = ddbtn;
                ddbtn.ContextMenu.Placement = PlacementMode.Bottom;
                ddbtn.ContextMenu.IsOpen = true;
            }
        }
    }
}
